package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.mapper.*;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class BtyKmmxzService extends ServiceImpl<BtyyCwKmmxzMapper, BtyyCwKmmxz> {
    @Resource
    private BtyyCwKmmxzMapper kmmxzMapper;

    /**
     * 获取现金日记账列表
     */
    public Page<Map<String, Object>> listXjrjz(CwbbParam cwbbParam) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.listXjrjz(page,cwbbParam);
    }
    /**
     * 获取银行营业额日记账列表
     */
    public Page<Map<String, Object>> listYhyye(CwbbParam cwbbParam) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.listYhyye(page,cwbbParam);
    }


    /**
     * 获取费用明细列表
     */
    public Page<Map<String, Object>> listFymx(CwbbParam cwbbParam) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.listFymx(page,cwbbParam);
    }

    /**
     * 获取费用明细列表（不分页）
     */
    public List<BtyyCwKmmxz> listExportFymx(CwbbParam cwbbParam) {

        return this.baseMapper.listExportFymx(cwbbParam);
    }
    /**
     * 获取现金日记账列表（不分页）
     */
    public List<BtyyCwKmmxz> listExportXjrjz(CwbbParam cwbbParam) {

        return this.baseMapper.listExportXjrjz(cwbbParam);
    }
    /**
     * 获取银行营业额列表（不分页）
     */
    public List<BtyyCwKmmxz> listExportYhyye(CwbbParam cwbbParam) {

        return this.baseMapper.listExportYhyye(cwbbParam);
    }
    /**
     * 导入科目明细账excel type 1.现金日记账2.银行营业额日记账3.费用明细
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchImportKmmxz(String fileName, MultipartFile file,String dm,Integer type) throws Exception {
        boolean notNull = false;
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }

        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        //excel中多个sheet表，获取所有sheet表的数量
        //int sheets = wb.getNumberOfSheets();
        for (int i = 0; i < 1; i++) {
            Sheet sheet = wb.getSheetAt(i);
            if (sheet != null) {
                notNull = true;
            }

            Row rowss = sheet.getRow(1);
            String rows=rowss.getCell(0).toString();
            String kjqj=rows.replaceAll("\\s*", "").substring(rows.indexOf(":") + 1).substring(0,7);
            String kjqjs=kjqj.replace('.','-');
            List<BtyyCwKmmxz>list=baseMapper.selectList(new QueryWrapper<BtyyCwKmmxz>().eq("dm",dm)
                    .eq("kjqj",kjqjs).eq("type",type));
            if(list.size()>0){
                baseMapper.delete(new QueryWrapper<BtyyCwKmmxz>().eq("dm",dm)
                        .eq("kjqj",kjqjs).eq("type",type));
            }

            int lastRowNum = sheet.getLastRowNum();
            for (int r = 3; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                for(int m=1;m<11;m++){
                    Cell cell = row.getCell(m);
                    if (ToolUtil.isNotEmpty(cell)) {
                        cell.setCellType(CellType.STRING);
                    }
                }
                String rq = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();
                String kmbm = row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue();
                String kmmc = row.getCell(3) == null ? "" : row.getCell(3).getStringCellValue();
                String pzzh = row.getCell(4) == null ? "" : row.getCell(4).getStringCellValue();
                String zy = row.getCell(5) == null ? "" : row.getCell(5).getStringCellValue();
                String dfkm = row.getCell(6) == null ? "" : row.getCell(6).getStringCellValue();
                String jf = row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue();
                String df = row.getCell(8) == null ? "" : row.getCell(8).getStringCellValue();
                String fx = row.getCell(9) == null ? "" : row.getCell(9).getStringCellValue();
                String ye = row.getCell(10) == null ? "" : row.getCell(10).getStringCellValue();
               BtyyCwKmmxz kmmxz=new BtyyCwKmmxz();
               kmmxz.setId(UUIDUtils.getId());
               kmmxz.setType(type);
               kmmxz.setRq(rq);
               kmmxz.setDm(dm);
               kmmxz.setKmbm(kmbm);
               kmmxz.setKmmc(kmmc);
               kmmxz.setPzzh(pzzh);
               kmmxz.setZy(zy);
               kmmxz.setDfkm(dfkm);
               kmmxz.setJf(jf);
               kmmxz.setDf(df);
               kmmxz.setFx(fx);
               kmmxz.setYe(ye);
               kmmxz.setKjqj(kjqjs);
               kmmxzMapper.insert(kmmxz);
            }
        }
        return notNull;
    }

}
